import { Injectable, OnModuleInit, Logger, Global } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PrismaClient } from '@prisma/client';


@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  private readonly logger = new Logger(PrismaService.name);

  constructor(configService: ConfigService) {
    //输出查询SQL等LOG
    super(
      configService.getOrThrow('NODE_ENV')
        ? { log: ['query', 'info', 'warn', 'error'] }
        : undefined,
    );
  }

  async onModuleInit() {
    await this.$connect().then(res => {
      this.logger.log('数据库连接成功')
    }).catch(err => {
      this.logger.error('数据库连接失败', err)
    })
  }


}
